//
//  HMTextAttachment.swift
//  EmoticonKeyboard
//
//  Created by 周英培 on 16/6/2.
//  Copyright © 2016年 周英培. All rights reserved.
//  自定义附件类

import UIKit

class HMTextAttachment: NSTextAttachment {
    
    var chs : String?
    // 将表情图片添加到属性文本当中
    class func emoticonImageToImageText(em: HMEmoticon,font: UIFont) -> NSAttributedString{
    
        let attachment = HMTextAttachment()
        //2.给附件对象添加图片
        attachment.image = UIImage(contentsOfFile: em.imagePath ?? "")
        //设置文本
        attachment.chs = em.chs
        
        let lineHeight = font.lineHeight
        attachment.bounds = CGRect(x: 0, y: -4, width: lineHeight, height: lineHeight)
        //3.通过附件对象 实例化属性字符串
        let imageText = NSMutableAttributedString(attributedString:  NSAttributedString(attachment: attachment))
        //3.3给属性文本添加font属性  但是不可变的属性文本是不能添加属性的
        imageText.addAttributes([NSFontAttributeName : font], range: NSMakeRange(0, 1))
        
        return imageText
    
    }
}
